package com.my.service.impl;

import java.util.List;

import com.my.dao.IBaseDao;
import com.my.entity.UserEntity;
import com.my.service.IUserService;

@SuppressWarnings("unchecked")
public class UserService implements IUserService {
	private IBaseDao baseDao;
	
	//用户注册
	public int userRegister(UserEntity userEntity) {
		try{
			baseDao.saveEntity(userEntity);
			return 1;
		}catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	//用户登录
	public UserEntity userLogin(String username,String password){
		UserEntity userEntity = getTitleAndValue("username", username.trim());
		if(userEntity == null){
			return null;
		}else{
			if(userEntity.getPassword().equals(password.trim())){
				return userEntity;
			}
			return null;
		}
	}
	
	//根据字段名和值查询
	public UserEntity getTitleAndValue(String title,Object name){
		List<UserEntity> list = baseDao.findByValue(UserEntity.class, title, name);
		if(list.size() != 0){
			return list.get(0);
		}
		return null;
	}
	
	
	
	
	public void setBaseDao(IBaseDao baseDao) {
		this.baseDao = baseDao;
	}
}
